########################################################################
#  Wiizard - A Wii games manager
#  Copyright (C) 2023  CYBERDEViL
#
#  This file is part of Wiizard.
#
#  Wiizard is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  Wiizard is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
########################################################################


from PyQt5.QtWidgets import QWidget, QSizePolicy
from PyQt5.QtCore import Qt, QRectF
from PyQt5.QtGui import QBrush, QColor, QPainter


class DiskUsageWidget(QWidget):
  def __init__(self, used=-1, capacity=-1, futureUsed=-1, free=-1, parent=None):
    QWidget.__init__(self, parent=parent)
    self.__used       = used
    self.__capacity   = capacity
    self.__futureUsed = futureUsed
    self.__free       = free

    self.text     = ""
    self.textRect = None
    self.fmt      = "{} GB / {} GB ({} GB free)"

    # Automatic expand width of self
    sizePolicy = self.sizePolicy()
    sizePolicy.setHorizontalPolicy(QSizePolicy.MinimumExpanding)
    self.setSizePolicy(sizePolicy)

    self.setMinimumHeight(24)
    self.remakeText()

  def remakeText(self):
    gbCapacity   = round(self.__capacity   / 1024, 2)
    gbUsed       = round(self.__used       / 1024, 2)
    #gbFutureUsed = round(self.__futureUsed / 1024, 2)
    gbFree       = round(self.__free       / 1024, 2)
    self.text    = self.fmt.format(gbUsed, gbCapacity, gbFree)

    fontMetrics   = self.fontMetrics()
    self.textRect = fontMetrics.tightBoundingRect(self.text)  # better but slower?

  def updateSizes(self, used, capacity, futureUsed, free):
    self.__used       = used
    self.__capacity   = capacity
    self.__futureUsed = futureUsed
    self.__free       = free

    if self.__capacity != -1:
      self.remakeText()

    self.update()

  def paintEvent(self, event):
    painter = QPainter(self)

    rounding = (3, 3)

    # Draw background
    painter.setPen(Qt.green)
    brush = QBrush(QColor(Qt.green), Qt.SolidPattern)
    painter.setBrush(brush)
    rect = QRectF(0.0, 0.0, self.width() - 1, self.height() - 1);
    painter.drawRoundedRect(rect, *rounding)

    if self.__capacity == -1:
      return

    # Draw future used
    if self.__futureUsed:
      width = (self.width() - 1) * ((self.__futureUsed / (self.__capacity / 100)) / 100)
      painter.setPen(Qt.yellow)
      brush = QBrush(QColor(Qt.yellow), Qt.Dense5Pattern)
      painter.setBrush(brush)
      rect = QRectF(0.0, 0.0, width, self.height() - 1);
      painter.drawRoundedRect(rect, *rounding)

    # Draw used
    if self.__used:
      width = (self.width() - 1) * ((self.__used / (self.__capacity / 100)) / 100)
      painter.setPen(Qt.red)
      brush = QBrush(QColor(Qt.red), Qt.Dense2Pattern)
      painter.setBrush(brush)
      rect = QRectF(0.0, 0.0, width, self.height() - 1)
      painter.drawRoundedRect(rect, *rounding)

    # Draw text background
    x = int(((self.width() - 1)  / 2) - ((self.textRect.width()  + 1)  / 2))
    y = int(((self.height() - 1) / 2) + ((self.textRect.height() + 1)  / 2))
    y -= 2
    painter.setPen(Qt.white)
    brush = QBrush(QColor(Qt.white), Qt.Dense4Pattern)
    painter.setBrush(brush)
    painter.drawRect(x, int(y/2) - 2, self.textRect.width(), self.textRect.height())

    # Draw text
    painter.setPen(Qt.black)
    painter.drawText(x, y, self.text)
